﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Data.SqlClient;

namespace MDA
{

    /// <summary>
    /// ReadXmlData ,用来从数据库读取数据并以XML的格式进行相应的处理.
    /// </summary>
    public class ReadXmlData
    {
        SqlConnection con = new SqlConnection(ConnectData.ConnectString);
        public ReadXmlData()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 到数据库读数据并以xml格式返回
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>从数据库读到xml格式的数据。</returns>
        public string Read(string sql)
        {
            string xml = "<?xml version=\"1.0\"?><DOCUMENTELEMENT>";
            SqlCommand com = new SqlCommand(sql, con);
            con.Open();
            XmlReader xr = com.ExecuteXmlReader();
            if (xr.Read())
                while (!xr.EOF)
                {
                    xml += xr.ReadOuterXml();
                }
            xml += "</DOCUMENTELEMENT>";
            con.Close();
            return xml;
        }

        /// <summary>
        /// 将DataTable的数据写成xml的方式返回。
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <returns></returns>
        public string Read(DataTable dt)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("<ROOT>");
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append("<TABLE>");
                foreach (DataColumn dc in dt.Columns)
                {
                    sb.Append("<" + dc.ColumnName.ToUpper() + ">");
                    sb.Append(dr[dc.ColumnName].ToString());
                    sb.Append("</" + dc.ColumnName.ToUpper() + ">");
                }

                sb.Append("</TABLE>");
            }

            sb.Append("</ROOT>");
            return sb.ToString();
        }

        /// <summary>
        /// 将DataTable的数据写成string的方式返回
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <returns></returns>
        public string DataTableToString(DataTable dt)
        {
            string flag = "true";
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            foreach (DataRow dr in dt.Rows)
            {                
                foreach (DataColumn dc in dt.Columns)
                {                   
                    sb.Append(dr[dc.ColumnName].ToString());
                    if (flag == "true")
                    {
                        sb.Append("|?|");
                    }
                    flag = "false";
                }
                sb.Append("|!|");
                flag = "true";
            }            
            return sb.ToString();
        }
    }

}